<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1">
	<title></title>
</head>
<body>
	<script type="text/javascript" src="jquery-3.2.1.js"></script>
	<script type="text/javascript">
		//浅拷贝：当b复制了a，b的儿子属性不会随a的变化而变化，但是从第二层开始至深都会开始改变

		let a = {
			name:"恩师",
			age:20,
			car: ["筋斗云", "凯美瑞"]
		}
		// 实现浅拷贝
		//1.for循环
		/*let b = {}
		function copy(arr1,arr2){
			for(let key in arr1){
				console.log(key)
				arr2[key] = arr1[key]
			}
		}
		copy(a,b)
		a.name = "猪八戒"
		a.car[0] = "奔驰"
		console.log(a)
		console.log(b)*/

		// 2.Object.assign(目标对象，源对象)
		/*let b = {}
		Object.assign(b, a)
		a.name = "猪八戒"
		a.car[0] = "奔驰"
		console.log(a)
		console.log(b)*/


		// 3.slice()
		let arr = [1, 2,[3, 4]]
		let b = []
		// b = arr.slice()
		// arr[0] = 66
		// arr[2][0] = 88
		// console.log(arr, b)


		// 4.contcat()
		// b = b.concat(arr)
		// arr[0] = 66
		// arr[2][0] = 88
		// console.log(arr, b)

		// 5.es6的剩余运算符 ...
		// b = [...arr]
		// arr[0] = 66
		// arr[2][0] = 88
		// console.log(arr, b)


		// 2.深拷贝： b复制a，修改a，b不会跟着改变，有独立的内存

		// 1.JSON.parse(JSON.stringify())
		// let c = {}
		// c = JSON.parse(JSON.stringify(a))
		// a.name = "猪八戒"
		// a.car[0] = "奔驰"
		// console.log(a)
		// console.log(b)


		// 2.递归函数
		
			let m = {
			name:"恩师",
			age:20,
			car: ["筋斗云", "凯美瑞"],
			dog:{
				name:"豆豆",
				age:2,
				color:"黑狗"
			}
		}
		/*function fn(a, b){
			for(let key in a){
				// key
				let item = a[key]
				// console.log(item)
				// 数组
				if(item instanceof Array){
					console.log(item)
					b[key] = []
					fn(item, b[key])
				}else if (item instanceof Object ){
					console.log(item, "======")
					b[key] = {}
					fn(item, b[key])
				}else{
					// 基本数据类型赋值
					b[key] = item
				}
			}
		}
		let n = []
		fn(m,n)
		m.dog.color = "黄狗"
		console.log(m,n)*/

		// 3.jquery中的extend方法
		//$.extend(true,n, m)//深拷贝
		//$.extend(n,m) 浅拷贝
		let n = {}
		$.extend(true,n, m)
		m.dog.color = "黄狗"
		console.log(m,n)
	</script>
</body>
</html>